/*
 * worker.cpp
 *
 *  Created on: Mar 2, 2013
 *      Author: RDU
 */

#include "worker.h"
#include <time.h>

WorkerThread::WorkerThread(MyFrame *pFrame):wxThread(wxTHREAD_DETACHED)
{
	m_pFrame=pFrame;
	m_camera=NULL;
	m_bLife=false;
	m_pMutex=new wxMutex();
}

WorkerThread::~WorkerThread()
{
	m_pMutex=NULL;
	m_pFrame=NULL;
}

void WorkerThread::OnExit()
{
	delete(m_pMutex);
}

void *WorkerThread::Entry()
{
	double duration,freq;
	clock_t time;

	m_bLife=true;

	while(m_bLife)
	{
		if(TestDestroy()==1)
			break;

		//work to do
		//AutoStitch stitcher;
		//IpassStitching stitcher;
		//m_pFrame->GetImagePanel()->DrawPano(stitcher.GetStitchedImage());

		m_camera->CamerasRun();

		if(m_pFrame->m_startStitching)
		{
			if(m_camera->m_Cam1_isOpen && m_camera->m_Cam2_isOpen)
			{
				if(m_pFrame->m_autoStitching)
				{
					m_autoStitcher.StitchImages(m_camera->m_CapturedImage1,m_camera->m_CapturedImage2);
					//m_pFrame->GetImagePanel3()->DrawPano(m_autoStitcher.GetStitchedImage());

					Mat src=imread("show_buff.jpg");
					m_pFrame->GetImagePanel3()->DrawPano(src);
				}
				else if(m_pFrame->m_homoStitching)
				{
					m_homoStitcher.StitchImages(m_camera->m_CapturedImage2,m_camera->m_CapturedImage1);
					m_pFrame->GetImagePanel3()->DrawPano(m_homoStitcher.GetStitchedImage());
				}

				//Show Update Frequency
				time=m_pFrame->m_timeLabel;
				m_pFrame->m_timeLabel=clock();
				duration=(double)(m_pFrame->m_timeLabel - time) / CLOCKS_PER_SEC;
				freq=1.0/duration;

				//cout<<"duration="<<duration<<endl;
				//cout<<"freq="<<freq<<endl;

				String status;
				ostringstream strs;
				strs << freq;
				String str = strs.str();
				status="Image Update Frequency: "+str+" Hz";
				m_pFrame->SetStatusBar(status);
			}
		}

		wxThread::Sleep(5);
	}

	return NULL;
}
